Import mojo
Import utoopia.vector
Import diddy

Class NumberButtons
	Field width
	Field height
	Field buttonWidth
	Field buttonHeight
	Field vector:Vector
	Field highlightNumber = 0
	Field pressedNumber:int = -1
	Field rows:Int = 5
	Field columns:Int = 3
	Field currentNumber:String
	Field inputNumber:String
	Field currentSign:String
	Field answer:Int = -1
	
	Method New(_vector:Vector, _width:Int, _height:Int)
		width = _width
		height = _height
		vector= _vector
		
		
		buttonHeight = (height-50)/rows
		buttonWidth = width/columns
	End
	
	Method Update()
		Local number:Int = 0
		Local mouseY:Float=MouseY()
		Local mouseX:Float=MouseX()
		pressedNumber = -1
		
		If (mouseX>vector.X)
			For Local y:Int = 1 To rows
				For Local x:Int = 1 To columns
					number += 1
					if(mouseX>x*buttonWidth-buttonWidth+vector.X And mouseX<x*buttonWidth+vector.X And mouseY>y*buttonHeight-buttonHeight+50 And mouseY< y*buttonHeight+50)
						highlightNumber = number
					End
					
				Next
			Next
		Endif
		
		If (MouseHit(MOUSE_LEFT)>0) And MouseX>SCREEN_WIDTH-width
			pressedNumber = highlightNumber		
			
			If(pressedNumber=13)
				If(currentSign="+")
						answer = Int(currentNumber)+Int(inputNumber)
					Else
						answer = Int(currentNumber)-Int(inputNumber)
				EndIf
			ElseIf(pressedNumber=14)
				Clear()
			ElseIf(pressedNumber>10)'sign button
				currentSign = GetInputAsString(pressedNumber)	
			Else	
				inputNumber += GetInputAsString(pressedNumber)	
			EndIf
			
			
		Else
			pressedNumber = -1
		End
	End
	
	
	Method Render()
		Local number:Int = 0
		Local text:String
		PushMatrix		
		SetAlpha (0.9)
		SetColor(0,0,0)
		DrawRect(vector.X, 0, width, 50 )
	
		
		DrawText (currentNumber+ " " +currentSign+ " " +inputNumber, vector.X, 20)
		For Local y:Int = 1 To rows
		For Local x:Int = 1 To columns
		
		
		number += 1
		
		SetAlpha (0.9)
		If(number=highlightNumber)
			SetColor(0,200,0)
		Else
			SetColor(200,0,0)
		endif
		DrawRect((x*buttonWidth)-buttonWidth+vector.X, y*buttonHeight-buttonHeight+50, buttonWidth, buttonHeight )
		
		
		
		DrawText(GetInputAsString(number), x*buttonWidth-buttonWidth/2+vector.X, y*buttonHeight-buttonHeight/2+50)

		Next
		Next
		PopMatrix
		
	End
	
	Method Clear:Void()
		currentSign = ""
		inputNumber = ""
		answer=-1
	End
	Method GetInputAsString:String(_input:Int)
	
	Select _input
		Case 10
			Return "0"
		Case 11
			Return "+"
		Case 12
			Return "-"
		Case 13
			Return "GO!"
		Case 14
			Return "C"	
		Case 15
			Return ""		
		Default
			Return _input
	End
	
	End
	
End


